package com.rongke.doctor.web.advice;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.doctor.constant.DoctorErrorCode;
import com.rongke.enums.MedicalRecordStatusEnum;
import com.rongke.model.MedicalAdvice;
import com.rongke.model.MedicalRecords;
import com.rongke.service.MedicalAdviceService;
import com.rongke.service.MedicalRecordsService;
import com.rongke.utils.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 精准治疗建议管理接口
 */
@RestController
@RequestMapping(value = "/api/doctor/advice")
@Transactional
public class DoctorAdviceController {
	private static final Logger log = Logger
			.getLogger(DoctorAdviceController.class);

	@Autowired
	private MedicalAdviceService medicalAdviceService;

	@Autowired
	private MedicalRecordsService medicalRecordsService;

	/**
	 * 根据病历ID查询未审核的精准治疗建议
	 * 
	 * @param medicalRecords
	 * @return
	 */
	@RequestMapping(value = "/queryUnVerified", method = RequestMethod.GET)
	public JsonResp queryUnVerifiedAdvice(String medicalRecordId) {
		log.debug("queryUnVerifiedAdvice, medicalRecordId=" + medicalRecordId);

		try {
			if (StringUtil.isEmpty(medicalRecordId)) {
				return JsonResp
						.fa(DoctorErrorCode.INVALID_PARAM.getErrorCode());
			}

			// 直接取最新的一次治疗建议，如果该治疗建议已经被审核过了，则报错
			EntityWrapper<MedicalAdvice> ew = new EntityWrapper<MedicalAdvice>();
			ew.eq("medical_record_id", medicalRecordId);
			ew.orderBy("gmt_create", false);// 按照生成时间倒序，取最近一次治疗建议
			ew.last("1");

			MedicalAdvice unverifiedAdvice = medicalAdviceService.selectOne(ew);
			if (null == unverifiedAdvice) {
				return JsonResp.fa(DoctorErrorCode.SYSTEM_ERROR.getErrorCode());
			}

			if (unverifiedAdvice.getGmtAuto() == null) {
				return JsonResp.fa(DoctorErrorCode.ADVICE_NOT_GENERATED
						.getErrorCode());
			}

			if (unverifiedAdvice.getGmtVerified() != null) {
				return JsonResp.fa(DoctorErrorCode.ADVICE_VERIFIED
						.getErrorCode());
			}

			return JsonResp.ok(unverifiedAdvice);
		} catch (Exception e) {
			log.error("queryUnVerifiedAdvice failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据病历ID查询精准治疗建议，只在病历状态=3时才会输出
	 * 
	 * @param medicalRecords
	 * @return
	 */
	@RequestMapping(value = "/verifyAdvice", method = RequestMethod.POST)
	public JsonResp verifyAdvice(@RequestBody MedicalAdvice medicalAdvice) {
		log.debug("verifyAdvice");

		try {
			if (null == medicalAdvice) {
				return JsonResp.fa(null);
			}

			MedicalAdvice adviceDo = new MedicalAdvice();
			adviceDo.setId(medicalAdvice.getId());
			adviceDo.setGmtModified(new Date());
			adviceDo.setVerifiedCheckAdvice(medicalAdvice
					.getVerifiedCheckAdvice());
			adviceDo.setVerifiedTreateAdvice(medicalAdvice
					.getVerifiedTreateAdvice());
			adviceDo.setVerifiedDoctorUserId(medicalAdvice
					.getVerifiedDoctorUserId());
			adviceDo.setGmtVerified(new Date());
			medicalAdviceService.updateById(adviceDo);

			MedicalRecords recordsDo = new MedicalRecords();
			recordsDo.setId(medicalAdvice.getMedicalRecordId());
			recordsDo.setStatus(MedicalRecordStatusEnum.VERIFIED.getStatus());
			medicalRecordsService.updateById(recordsDo);

			JSONObject json = new JSONObject();
			json.put("id", medicalAdvice.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			log.error("queryByUserId failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 医生审核前，需要获取自动生成的治疗建议
	 */
	@RequestMapping(value = "/queryVerifiedAdvice", method = RequestMethod.GET)
	public JsonResp queryVerifiedAdvice(Long adviceId, String doctorUserId) {
		log.debug("queryLastAdvice");

		try {
			if (StringUtil.isEmpty(doctorUserId)) {
				return JsonResp.fa(null);
			}

			MedicalAdvice advice = medicalAdviceService.selectById(adviceId);
			if (advice == null) {
				return JsonResp.fa(DoctorErrorCode.SYSTEM_ERROR.getErrorCode());
			}

			if (!doctorUserId.equals(advice.getVerifiedDoctorUserId())) {
				return JsonResp.fa(DoctorErrorCode.NOT_VERIFIED_DOCTOR
						.getErrorCode());
			}

			return JsonResp.ok(advice);
		} catch (Exception e) {
			log.error("queryLastAdvice failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据病历ID查询精准治疗建议，只在病历状态=3时才会输出<br>
	 * TODO:应该输出病历信息
	 * 
	 * @param medicalRecords
	 * @return
	 */
	@RequestMapping(value = "/queryAdviceHis", method = RequestMethod.GET)
	public JsonResp queryAdviceHistory(Page<Map<String, Object>> page,
			String doctorUserId) {
		log.debug("queryAdviceHistory");

		try {
			if (StringUtil.isEmpty(doctorUserId)) {
				return JsonResp.fa(null);
			}

			Page<Map<String, Object>> result = medicalRecordsService
					.selectVerifyHistory(page, doctorUserId);

			return JsonResp.ok(result);
		} catch (Exception e) {
			log.error("queryByUserId failed", e);
			return JsonResp.fa(null);
		}
	}
}
